package com.zhan.signalling.data.listener;

import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.listener.DataListener;
import com.zhan.signalling.check.AccoutPermissionCheck;
import com.zhan.signalling.commons.Constants;
import com.zhan.signalling.commons.SocketEvent;
import com.zhan.signalling.message.Message;
import com.zhan.signalling.message.Result;

/**
 * 退出房间事件
 * @author knight
 *
 */
public class ExitGroupListener implements DataListener<Message> {

	public void onData(SocketIOClient client, Message data, AckRequest ackSender) throws Exception {
		
		// 校验权限
		AccoutPermissionCheck.check(client, SocketEvent.EXIT_GROUP_EVENT);
		
		String groupId = client.get(Constants.GROUP_KEY);
		client.leaveRoom(groupId);
		JSONObject user = client.get(Constants.USER_KEY);
		client.getNamespace().getRoomOperations(groupId)
								.sendEvent(SocketEvent.EXIT_GROUP_EVENT, 
										new Result(user, Constants.SUCCESS));
		
	}

}
